/**
 * Created by BestLala on 6/21/2017.
 */
$(function () {
    registerformdate();

    registerformdaatetime();

    $('.timepicker input').timepicker({
        showMeridian:false
    });

    $(".file-input-field").each(function () {
        var $fileuploader= $(this).find("input.file-uploader");
        $fileuploader.change(function () {
            var relativeinput = $(this).attr("data-name");
            $(relativeinput).val(this.value);
           $(this).parents(".file-input-field").find(".file-caption-name").html(this.value);
        });
    });
    registertimepicker(".form_timepicker");

    toastr.options = {
        "closeButton": true,
        "debug": false,
        "newestOnTop": false,
        "progressBar": false,
        "positionClass": "toast-top-right",
        "preventDuplicates": false,
        "onclick": null,
        "showDuration": "1000",
        "hideDuration": "1000",
        "timeOut": "5000",
        "extendedTimeOut": "1000",
        "showEasing": "swing",
        "hideEasing": "swing",
        "showMethod": "show"
    }

    $form=$("form[method='get']");
    $("button[type='reset']").click(function(){
        $form.find("input[type='text'],select").each(function () {
            $(this).val("");
        });
        $form.submit();
    });

    $("a").click(function () {
        window.isdownload=true;
        setTimeout(function(){
            window.isdownload=false;
        },1000);
    });
    window.onbeforeunload=function(e) {
        if (!window.isdownload) {
            showloading();
        }
    };
})
function formatRepo (repo) {
    if (repo.loading) return repo.text;

    var markup = "<div class='select2-result-repository clearfix'>" +
        "<div class='select2-result-repository__avatar'><img src='" + repo.owner.avatar_url + "' /></div>" +
        "<div class='select2-result-repository__meta'>" +
        "<div class='select2-result-repository__title'>" + repo.full_name + "</div>";

    if (repo.description) {
        markup += "<div class='select2-result-repository__description'>" + repo.description + "</div>";
    }

    markup += "<div class='select2-result-repository__statistics'>" +
        "<div class='select2-result-repository__forks'><i class='fa fa-flash'></i> " + repo.forks_count + " Forks</div>" +
        "<div class='select2-result-repository__stargazers'><i class='fa fa-star'></i> " + repo.stargazers_count + " Stars</div>" +
        "<div class='select2-result-repository__watchers'><i class='fa fa-eye'></i> " + repo.watchers_count + " Watchers</div>" +
        "</div>" +
        "</div></div>";

    return markup;
}

function formatRepoSelection (repo) {
    return repo.full_name || repo.text;
}

function showloading() {
    $(".preloader,.loader_img>img").show();
}

function hideloading() {
    $(".preloader,.loader_img>img").hide();
}

function registerformdate() {
    $('.form_date').each(function () {
        var config = {
            format:"yyyy-mm-dd",
            language:  'zh-CN',
            weekStart: 1,
            todayBtn:  1,
            autoclose: 1,
            todayHighlight: 1,
            startView: 2,
            minView: 2,
            forceParse: 0,
            pickerPosition:"bottom-left",
        };

        $(this).datetimepicker(config);
    });
}

function registerformdaatetime() {
    $('.form_datetime').each(function () {
        var config = {
            format:"yyyy-mm-dd hh:ii:ss",
            language:  'zh-CN',
            todayBtn:  1,
            autoclose: 1
        };

        $(this).datetimepicker(config);
    });
}

function registertimepicker(selector) {
    $(".notmarked "+selector).timepicker({
        showMeridian: false,
        showSeconds: false,
        snapToStep: true,
        minuteStep: 1,
        format:"HH:MM"
    }).on('changeTime.timepicker', function (e) {
        $element = $(e.target || e.srcElement);
    });
}

var cbvm={
    message:{
        success:function (msg) {
            toastr["success"](msg, "");
        },
        error:function (msg) {
            toastr["error"](msg, "");
        }
    },
    helper: {
        validemail: function (value) {
            var result = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i.test(value);
            return result;
        },
        isinteger: function (input) {
            var result = /^(-?\d+)/i.test(input);

            return result;
        },
        isnumeric: function (input) {
            var result = $.isNumeric(input);

            return result;
        }
    }
}